home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Eudora 1.3.1 / source / lineio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  3.7 KB  |  167 lines  |  [TEXT/MPS ]

  1. #define FILE_NUM 20
  2. /* Copyright (c) 1990-1992 by the University of Illinois Board of Trustees */
  3. #pragma load EUDORA_LOAD
  4. #pragma segment Lib
  5. static UHandle Buffer=nil;     /* where our characters go */
  6. static long BufferSize;         /* how big our buffer is */
  7. static long BFilled=0;            /* the number of chars in the buffer */
  8. static short RefN=0;                    /* the path number of the open file */
  9. static long BSpot;                    /* index of next character to transfer */
  10. static long LastSpot;             /* the file position of beginning of
  11.                                                                 the last line read */
  12. static long FSpot;                    /* the position in the file of the start
  13.                                                                 of the buffer */
  14.                                                                 
  15. #define LIKE_BUFFER 4096
  16.  
  17. short OpenLine(short vRef,long dirId,UPtr name)
  18. {
  19.     int err;
  20.     
  21.     CloseLine();
  22.         
  23.     /*
  24.      * open the file
  25.      */
  26.     if (err=FSHOpen(name,vRef,dirId,&RefN,fsRdWrPerm)) goto failure;
  27.     
  28.     /*
  29.      * allocate a buffer
  30.      */
  31.     GetEOF(RefN,&BufferSize);
  32.     BufferSize += 2;
  33.     if (BufferSize > LIKE_BUFFER) BufferSize = LIKE_BUFFER;
  34.     if ((Buffer=NuHandle(BufferSize))==nil)
  35.     {
  36.         err=MemError();
  37.         goto failure;
  38.     }
  39.     
  40.     /*
  41.      * fill the first buffer
  42.      */
  43.     BFilled = BufferSize-1;
  44.     LDRef(Buffer);
  45.     err=FSRead(RefN,&BFilled,*Buffer);
  46.     UL(Buffer);
  47.     if (err && err!=eofErr) goto failure;
  48.     BSpot = LastSpot = FSpot = 0;
  49.     (*Buffer)[BFilled] = '\n';            /* a marker, to expedite searches */
  50.     return(noErr);
  51.  
  52. failure:
  53.     CloseLine();
  54.     return(err);
  55. }
  56.  
  57. /************************************************************************
  58.  * SeekLine - seek the line routines to a given spot
  59.  ************************************************************************/
  60. int SeekLine(long spot)
  61. {
  62.     int err;
  63.     
  64.     if (err = SetFPos(RefN,fsFromStart,spot)) goto failure;
  65.     
  66.     /*
  67.      * fill the first buffer
  68.      */
  69.     BFilled = BufferSize-1;
  70.     LDRef(Buffer);
  71.     err=FSRead(RefN,&BFilled,*Buffer);
  72.     UL(Buffer);
  73.     if (err && err!=eofErr) goto failure;
  74.     BSpot = 0;
  75.     LastSpot = FSpot = spot;
  76.     (*Buffer)[BFilled] = '\n';            /* a marker, to expedite searches */
  77.     return(noErr);
  78.  
  79. failure:
  80.     CloseLine();
  81.     return(err);
  82. }
  83. /**********************************************************************
  84.  * GetLine - read a line of a given size.  returns 0 for eof, negative
  85.  * for file manager errors, LINE_START if returning the beginning of
  86.  * a line, LINE_MIDDLE if a partial line is being returned.
  87.  **********************************************************************/
  88. int GetLine(UPtr line,int size)
  89. {
  90.     register UPtr bp;
  91.     register UPtr cp=line;
  92.     int where;
  93.     int err;
  94.     static FILE *trace=0;
  95.     
  96.     if (!BFilled) return (0);         /* we have no chars */
  97.     CycleBalls();
  98.     bp = LDRef(Buffer) + BSpot;
  99.     where = (bp==*Buffer || bp[-1]=='\n') ? LINE_START : LINE_MIDDLE;
  100.     LastSpot = FSpot + BSpot;     /* remember where this line begins */
  101.     for (;;)
  102.     {
  103.         while (*bp!='\n' && --size>0) *cp++ = *bp++;
  104.         BSpot = bp - *Buffer;
  105.         if (BSpot==BFilled)
  106.         {
  107.             FSpot += BFilled;
  108.             BFilled = BufferSize - 1;
  109.             err=FSRead(RefN,&BFilled,*Buffer);
  110.             if (err==eofErr)
  111.             {
  112.                 if (BFilled==0)
  113.                 {
  114.                     *cp = 0;
  115.                     BFilled = 0;
  116.                     UL(Buffer);
  117.                     return (where);
  118.                 }
  119.             }
  120.             else if (err)
  121.             {
  122.                 UL(Buffer);
  123.                 FileSystemError(READ_MBOX,"",err);
  124.                 return(err);
  125.             }
  126.             (*Buffer)[BFilled] = '\n';    /* a marker, to expedite searches */
  127.             BSpot = 0;
  128.             bp = *Buffer;
  129.         }
  130.         else
  131.         {
  132.             if (size>0)
  133.             {
  134.                 *cp++ = '\n';
  135.                 BSpot++;
  136.             }
  137.             *cp = 0;
  138.             UL(Buffer);
  139.             return(where);
  140.         }
  141.     }
  142. }
  143.  
  144. /**********************************************************************
  145.  * CloseLine - shut up shop.    Calling it on closed file does no harm.
  146.  **********************************************************************/
  147. void CloseLine(void)
  148. {
  149.     if (RefN != 0)
  150.     {
  151.         FSClose(RefN);
  152.         RefN = 0;
  153.     }
  154.     if (Buffer != nil)
  155.     {
  156.         DisposHandle(Buffer);
  157.         Buffer = nil;
  158.     }
  159.     BFilled = 0;
  160. }
  161.  
  162. long TellLine(void)
  163. {
  164.     return (LastSpot);
  165.  
  166.